﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;

namespace YiSha.Service.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-07 14:34
    /// 描 述：库存号牌服务类
    /// </summary>
    public class SparePlateService : RepositoryFactory
    {
        #region 获取数据

        public async Task<List<SparePlateEntity>> GetList(SparePlateListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<SparePlateEntity>> GetPageList(SparePlateListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<SparePlateEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<SparePlateEntity>(id);
        }

        #endregion 获取数据

        #region 提交数据

        public async Task SaveForm(SparePlateEntity entity)
        {
            var db = this.BaseRepository();
            if (entity.Id.IsNullOrZero())
            {
                if (db.IQueryable<SparePlateEntity>(t => t.HuanBaoCode == entity.HuanBaoCode).Count() > 0 || db.IQueryable<MechanicsEntity>(t => t.HuanBaoCode == entity.HuanBaoCode).Count() > 0)
                {
                    throw new Exception("该环保号牌已存在");
                }
                entity.Create();
                await db.Insert(entity);
            }
            else
            {
                if (db.IQueryable<SparePlateEntity>(t => t.HuanBaoCode == entity.HuanBaoCode).Count() > 0 || db.IQueryable<MechanicsEntity>(t => t.HuanBaoCode == entity.HuanBaoCode).Count() > 0)
                {
                    throw new Exception("该环保号牌已存在");
                }
                await db.Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<SparePlateEntity>(idArr);
        }

        #endregion 提交数据

        #region 私有方法

        private Expression<Func<SparePlateEntity, bool>> ListFilter(SparePlateListParam param)
        {
            var expression = LinqExtensions.True<SparePlateEntity>();
            if (param != null)
            {
                if (!param.region.IsNullOrEmpty() && param.region != "408")
                {
                    expression = expression.And(t => t.HuanBaoCode.Contains(param.region));
                }
                if (!param.keywords.IsNullOrEmpty())
                {
                    expression = expression.And(t => t.HuanBaoCode.Contains(param.keywords));
                }
            }
            return expression;
        }

        #endregion 私有方法
    }
}
